package cn.jbolt.core.captcha;

import cn.jbolt.core.base.config.JBoltConfig;
import cn.jbolt.core.cache.JBoltCacheKit;
import cn.jbolt.core.consts.JBoltConst;
import cn.jbolt.core.kit.JBoltSaasTenantKit;
import com.jfinal.captcha.Captcha;
import com.jfinal.captcha.ICaptchaCache;

/**
 * 验证码缓存管理工具类
 */
public class JBoltCaptchaCache implements ICaptchaCache {
    @Override
    public void put(Captcha captcha) {
        String key = JBoltConst.JBOLT_CAPTCHA_CACHE_PREFIX+captcha.getKey();
        if(JBoltConfig.SAAS_ENABLE && JBoltSaasTenantKit.me.isOk()) {
           key = JBoltSaasTenantKit.me.getSn()+"_"+key;
        }
        JBoltCacheKit.put(JBoltConfig.JBOLT_CACHE_NAME,key,captcha,Captcha.DEFAULT_EXPIRE_TIME);
    }

    @Override
    public Captcha get(String key) {
        String newKey = JBoltConst.JBOLT_CAPTCHA_CACHE_PREFIX+key;
        if(JBoltConfig.SAAS_ENABLE && JBoltSaasTenantKit.me.isOk()) {
            newKey = JBoltSaasTenantKit.me.getSn()+"_"+newKey;
        }
        return JBoltCacheKit.get(JBoltConfig.JBOLT_CACHE_NAME,newKey);
    }

    @Override
    public void remove(String key) {
        String newKey = JBoltConst.JBOLT_CAPTCHA_CACHE_PREFIX+key;
        if(JBoltConfig.SAAS_ENABLE && JBoltSaasTenantKit.me.isOk()) {
            newKey = JBoltSaasTenantKit.me.getSn()+"_"+newKey;
        }
        JBoltCacheKit.remove(JBoltConfig.JBOLT_CACHE_NAME,newKey);
    }

    @Override
    public void removeAll() {

    }
}
